<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>11_自定义JS对象</title>
</head>
<body>
<table border="1px">
    <caption>商品表</caption>
    <tr>
        <th>商品标题</th>
        <th>商品价格</th>
        <th>商品库存</th>
    </tr>
</table>
<script>
    //定义数组，模拟从后端请求回来的商品数据
    let productsArr = [
        {title:'华为',price:8000,num:500},
        {title:'小米',price:7000,num:400},
        {title:'三星',price:9000,num:650},
        {title:'苹果',price:10000,num:900}
    ];
    //1.获取表格元素
    let tableE = document.querySelector('table');
    //2.遍历商品数组
    for (let i = 0; i < productsArr.length; i++){
        //3.创建1个tr和3个td
        let tr = document.createElement('tr');
        let titleTd = document.createElement('td');
        let priceTd = document.createElement('td');
        let numTd = document.createElement('td');
        //4.将当前遍历到的商品数据添加到td中
        titleTd.innerHTML = productsArr[i].title;
        priceTd.innerHTML = productsArr[i].price;
        numTd.innerHTML = productsArr[i].num;
        //5.将td添加到tr中
        tr.append(titleTd,priceTd,numTd);
        //6.将tr添加到table中
        tableE.append(tr);
    }

    //1.定义一个js对象
    let p1 = {
        name:'张三',
        age:18,
        say:function(){
            console.log('我是'+this.name+',今年'+this.age+'岁');
        }
    }
    p1.say();
    console.log(p1.name,p1.age);
    console.log(p1);

    //2.先定义一个空对象,再动态向其添加属性与方法
    let p2 = {};
    p2.name = '李四';
    p2.age = 19;
    p2.say = function(){
        console.log('我是'+this.name+',今年'+this.age+'岁');
    }
    p2.say();
    console.log(p2.name,p2.age);
    console.log(p2);
</script>
</body>
</html>